package com.lanou.tipsy.fragment.recommend;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.recommend.NewestSongAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.entity.recommend.newest_recommend.newSongsRecommend.NewSongsEveryday;
import com.lanou.tipsy.entity.recommend.newest_recommend.newSongsRecommend.NewSongsRecommend;
import com.lanou.tipsy.entity.songInfo.SongInfoRoot;
import com.lanou.tipsy.tools.volley.ResponseErrorListener;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by FengJun on 15/11/5.
 * 推荐->最新歌曲->每日新歌推荐->详情
 */
public class EverydayNeweastSongsFragment extends BaseFragment implements View.OnClickListener {
    private static final String TAG = "EverydayNeweastSongsFragment";
    private Context context;
    private ArrayList<NewSongsEveryday.ContentEntity> entities;//实体类集合
    private NewSongsEveryday.ContentEntity entity;//实体类
    private NewestSongAdapter adapter;//适配器
    private RecyclerView newestRv;//列表
    private SimpleDraweeView draweeView;//顶部图片
    private Toolbar toolbar;
    private NewSongsRecommend newSongsRecommend;//实体类
    private VolleySingleton volleySingleton;
    private StringRequest request,request2;
    private ResponseErrorListener errorListener;
    private TextView songsNumTv, songsTagTv;//歌曲数量,tag

    private SongInfoRoot root;//网络歌曲url实体类


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_recommend_new_songs_everyday, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(getView());
        //接受上一个页面传过来的数据
        Bundle bundle = getArguments();
        newSongsRecommend = (NewSongsRecommend) bundle.getSerializable("newsongs");
        String imgUrl = newSongsRecommend.getCollection().getPic();
        if (imgUrl != null) {
            Uri uri = Uri.parse(imgUrl);
            draweeView.setImageURI(uri);
        }
        //通过上个页面传递过来的数据劈接网址,拉取数据
        String listId = newSongsRecommend.getCollection().getList_id();
        String url = Constants.NEWEST_SONGS_RECOMMEND_URL + listId;
        Log.i("ccc",url);
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                NewSongsEveryday newSongsEveryday = JSON.parseObject(response, NewSongsEveryday.class);
                entities = newSongsEveryday.getContent();
                songsNumTv.setText("共" + entities.size() + "首歌");
                songsTagTv.setText(newSongsEveryday.getDesc());
                adapter = new NewestSongAdapter(context, entities);
                newestRv.setAdapter(adapter);
                newestRv.setLayoutManager(new LinearLayoutManager(context));
                newestRv.setItemAnimator(new DefaultItemAnimator());
                Uri uri = Uri.parse(newSongsEveryday.getPic_500());
                Log.i("每日推荐大图片URL", newSongsEveryday.getPic_500());
                draweeView.setImageURI(uri);

                click();
            }
        }, errorListener);
        volleySingleton.addQueue(request, TAG);
    }

    //private  String songUrl;
    private void click() {
        adapter.setOnItemClickLitener(new NewestSongAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
               /* String songid = entities.get(position).getSong_id();
                String newUrl = Constants.SERVICE_NET_SONGS_URL_START + songid + Constants.SERVICE_NET_SONGS_URL_END;
                Log.i("newurl", newUrl);
                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(newUrl, null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null) {
                            Log.i("tests", TAG + "歌单首页" + response + "");
                            String str = response.toString();
                            SongInfoRoot root = JSON.parseObject(str, SongInfoRoot.class);
                            String songUrl = root.getSongurl().getUrl().get(0).getFile_link();
                            String lrcUrl = root.getSonginfo().getLrclink();
                            Log.i("test", TAG + "->歌曲url:" + songUrl);
                            Log.i("test", TAG + "->LRC.lrc:" + lrcUrl);
                            String rot = root.toString();
                            //跳转至频谱页面
                            Intent intent = new Intent(getActivity(), SpectrumActivity.class);
                            intent.putExtra("url",rot );
                            //Log.i("newUrl", songUrl);
                            startActivity(intent);

                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });
                volleySingleton.addQueue(jsonObjectRequest, TAG);*/
                Toast.makeText(context,  ":此歌曲无法播放~", Toast.LENGTH_SHORT).show();
            }
        });
    }

    //初始化控件->监听
    private void initView(View view) {
        volleySingleton = VolleySingleton.getInstance();
        errorListener = new ResponseErrorListener();
        draweeView = (SimpleDraweeView) view.findViewById(R.id.draw_new_songs);
        toolbar = (Toolbar) view.findViewById(R.id.tl_new_songs);
        newestRv = (RecyclerView) view.findViewById(R.id.rv_new_songs);
        songsNumTv = (TextView) view.findViewById(R.id.tv_new_songs_count);
        songsTagTv = (TextView) view.findViewById(R.id.tv_new_songs_tag);
        toolbar.setNavigationIcon(R.mipmap.btn_web_back_normal);
        toolbar.setNavigationOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        getActivity().getSupportFragmentManager().popBackStack();
    }

    @Override
    public void onStop() {
        super.onStop();
        volleySingleton.removeRequest(TAG);
    }
}
